import { message } from 'antd';
import moment from 'moment';
import {
  getSendChart,
  getReceChart,
  getChargChart,
  getChargOrder,
  getPayChart,
  getPayOrder,
} from '@/services/analysisns';

export default {
  state: {
    // 发单
    sendData: [],
    sendMonth: [moment().subtract(11, 'months'), moment()],
    sendStartTime: moment()
      .subtract(11, 'months')
      .format('YYYY-MM'),
    sendEndTime: moment().format('YYYY-MM'),
    sendAddTotal: 0,
    sendRedTotal: 0,

    // 接单
    receData: [],
    receMonth: [moment().subtract(11, 'months'), moment()],
    receStartTime: moment()
      .subtract(11, 'months')
      .format('YYYY-MM'),
    receEndTime: moment().format('YYYY-MM'),
    receAddTotal: 0,
    receRedTotal: 0,

    // 收费
    chargData: [],
    chargMonth: [moment().subtract(11, 'months'), moment()],
    chargStartTime: moment()
      .subtract(11, 'months')
      .format('YYYY-MM'),
    chargEndTime: moment().format('YYYY-MM'),
    chargOrderData: [],
    chargTotal: 0,

    // 付费
    payData: [],
    payMonth: [moment().subtract(11, 'months'), moment()],
    payStartTime: moment()
      .subtract(11, 'months')
      .format('YYYY-MM'),
    payEndTime: moment().format('YYYY-MM'),
    payOrderData: [],
    payTotal: 0,
  },
  effects: {
    // 获取发单量趋势图
    *fetchSendChart({ payload }, { call, put }) {
      yield put({
        type: 'updateModelData',
        payload: {
          sendData: [],
          sendAddTotal: 0,
          sendRedTotal: 0,
        },
      });
      const response = yield call(getSendChart, payload);
      if (response.success) {
        if (response.data && response.data.chartList && response.data.chartList.length) {
          const addEmpData = [];
          response.data.chartList.forEach(item => {
            addEmpData.push({
              type: '增员',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.addNum || 0,
            });
          });
          const redEmpData = [];
          response.data.chartList.forEach(item => {
            redEmpData.push({
              type: '减员',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.delNum || 0,
            });
          });
          yield put({
            type: 'updateModelData',
            payload: {
              sendData: [...addEmpData, ...redEmpData],
              sendAddTotal: response.data.addTotalNum,
              sendRedTotal: response.data.delTotalNum,
            },
          });
        } else {
          message.warning('该月份下未查询到发单数据！');
        }
      } else {
        message.warning(response.message || '获取发单图表数据：服务端错误');
      }
    },
    // 获取接单量趋势图
    *fetchReceChart({ payload }, { call, put }) {
      yield put({
        type: 'updateModelData',
        payload: {
          receData: [],
          receAddTotal: 0,
          receRedTotal: 0,
        },
      });
      const response = yield call(getReceChart, payload);
      if (response.success) {
        if (response.data && response.data.chartList && response.data.chartList.length) {
          const addEmpData = [];
          response.data.chartList.forEach(item => {
            addEmpData.push({
              type: '增员',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.addNum || 0,
            });
          });
          const redEmpData = [];
          response.data.chartList.forEach(item => {
            redEmpData.push({
              type: '减员',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.delNum || 0,
            });
          });
          yield put({
            type: 'updateModelData',
            payload: {
              receData: [...addEmpData, ...redEmpData],
              receAddTotal: response.data.addTotalNum,
              receRedTotal: response.data.delTotalNum,
            },
          });
        } else {
          message.warning('该月份下未查询到接单数据！');
        }
      } else {
        message.warning(response.message || '获取接单图表数据：服务端错误');
      }
    },
    // 收费趋势图
    *fetchChargChart({ payload }, { call, put }) {
      yield put({
        type: 'updateModelData',
        payload: {
          chargData: [],
          chargTotal: 0,
        },
      });
      const response = yield call(getChargChart, payload);
      if (response.success) {
        if (response.data && response.data.chartList && response.data.chartList.length) {
          const chargData = [];
          response.data.chartList.forEach(item => {
            chargData.push({
              type: '收费：',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.chrgFee || 0,
            });
          });
          yield put({
            type: 'updateModelData',
            payload: {
              chargData,
              chargTotal: response.data.totalFee,
            },
          });
        } else {
          message.warning('该月份下未查询到收费数据！');
        }
      } else {
        message.warning(response.message || '获取收费图表数据：服务端错误');
      }
    },
    //  收费排行
    *fetchChargOrder({ payload }, { call, put }) {
      const response = yield call(getChargOrder, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          chargOrderData: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              chargOrderData: response.data,
            },
          });
        } else {
          message.warning('该月份下未查询到收费数据！');
        }
      } else {
        message.warning(response.message || '获取收费排行数据：服务端错误');
      }
    },
    // 付费趋势图
    *fetchPayChart({ payload }, { call, put }) {
      yield put({
        type: 'updateModelData',
        payload: {
          payData: [],
          payTotal: 0,
        },
      });
      const response = yield call(getPayChart, payload);
      if (response.success) {
        if (response.data && response.data.chartList && response.data.chartList.length) {
          const payData = [];
          response.data.chartList.forEach(item => {
            payData.push({
              type: '付费：',
              month: moment(item.staYm).format('YYYY-MM'),
              num: item.payFee || 0,
            });
          });
          yield put({
            type: 'updateModelData',
            payload: {
              payData,
              payTotal: response.data.totalFee,
            },
          });
        } else {
          message.warning('该月份下未查询到付费数据！');
        }
      } else {
        message.warning(response.message || '获取付费图表数据：服务端错误');
      }
    },
    //  付费排行
    *fetchPayOrder({ payload }, { call, put }) {
      const response = yield call(getPayOrder, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          payOrderData: [],
        },
      });
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              payOrderData: response.data,
            },
          });
        } else {
          message.warning('该月份下未查询到付费数据！');
        }
      } else {
        message.warning(response.message || '获取付费排行数据：服务端错误');
      }
    },
  },
  reducers: {
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
